//let baseUrl = "http://49.234.158.52:8080/"
$(function(){
    getUrlParmenter();

    $("#videoModal").find(".close").on("click", function () {
        videoObj.pause();
    })
});


// let videoObj = videojs("myvideo");
let videoObj = videojs("myvideo");

//获取url参数，请求数据
let courseId = null;



function getUrlParmenter() {
    let url = location.search;
    if (url.indexOf("?") != -1) {
        courseId = url.substr(1);
        // console.log(courseId)
        getCourseById(courseId);
        comment(courseId)
    }
}

//根据课程id获取课程
let course = null;

function getCourseById(courseId) {
    $.post({
        url: baseUrl + "education_api/course/queryByCourseId",
        data: {courseId: courseId},
        success: function (result) {
            // course = JSON.parse(result);
            course = result
            applyCourse(result);
        }
    },'json');
}

//渲染课程详情
function applyCourse(result){

    let obj = result.data;
    let array = obj.chapterList;

    let personNunber = 0;
    for (let i = 0; i < array.length; i++) {
        personNunber += array[i].viewCount;
        let chapterTemplate = $(".chapterTemplate").html()
            .replaceAll("{{sequence}}", array[i].sequence)
            .replaceAll("{{title}}", array[i].title)
            .replaceAll("{{videoUrl}}", array[i].videoUrl)
            .replaceAll("{{id}}", array[i].id);
        $("#labs").append(chapterTemplate);


    }

    let o = $(".chapters");
    console.log(o.html())


    let infoTemplate = $(".infoTemplate").html().replaceAll("{{title}}", obj.title)
        .replaceAll("{{synopsis}}", obj.synopsis)
        .replaceAll("{{personNumber}}", personNunber);

    $(".info-content").append(infoTemplate);

    let btnContainer = $(".btnContainer");
    if (obj.charge == 1) {
        $(".price").html("￥" + obj.money);
        let btnTemplate = $(".RedBtnTemplate").html();
        btnContainer.append(btnTemplate);
    } else if (obj.charge == 0) {
        let btnTemplate = $(".BlueBtnTemplate").html().replaceAll("{{videoUrl}}", array[0].videoUrl)
            .replaceAll("{{title}}", array[0].title)
            .replaceAll("{{id}}", array[0].id);
        btnContainer.append(btnTemplate);
    } else {
        let btnTemplate = $(".YellowBtnTemplate").html().replaceAll("{{videoUrl}}", array[0].videoUrl)
            .replaceAll("{{title}}", array[0].title)
            .replaceAll("{{id}}", array[0].id);
        btnContainer.append(btnTemplate);
    }

}

//点击购买按钮
function onPurchase() {
    $.post({
        url: baseUrl + "education_api/orders/insOrders",
        data: {courseId: courseId},
        success: function (result) {

            applyOrder(result);
        }
    }, "json");
}

//判断订单是否支付成功
function applyOrder(data){

    let result = data;

    if (typeof result == 'string') {
        result = JSON.parse(result);
    }


    if (result.code == 0) {
        location.reload()
    } else if (result.code == -2) {
        alert(result.message);
        location.href = "../index.html";
    } else {
        alert(result.message);
    }
}

//播放视频
function onPlay(obj) {
    console.log(course);
    if (course.data.charge == 1) {
        alert("该课程为付费课程，请购买后观看");
        return;
    }


    let vedioUrl = $(obj).attr("value");
    let chapterId = $(obj).attr("data-chapterId");
    let chapterTitle = $(obj).attr("data-chapterTitle");
    $("#videoModalLabel").html(chapterTitle)
    videoObj.ready(function () {
        this.src(baseUrl + "education_api/download/play?filename="+vedioUrl);
    });

    $.post({
        xhrFields:{
            withCredentials:true
        },
        url: baseUrl + "education_api/chapter/updateViewCount",
        data: {id: chapterId},
        success: function (result) {
            // let msg = JSON.parse(result).message;
            console.log(result.message)
        }
    }, 'json');

    $.post({
        xhrFields:{
            withCredentials:true
        },
        url: baseUrl + "education_api/studyTrace/insertStudyTrace",
        data: {courseId: courseId},
        success: function (result) {
            // let msg = JSON.parse(result).message;
            console.log(result.message)
        }
    }, 'json');

    $("#videoModal").modal("show");

}


//评论内容
function comment(courseId) {

    let commentContent = null;
    console.log(232332);
    $.ajax({
        xhrFields:{
            withCredentials:true
        },
        url: baseUrl + "education_api/courseComment/content",
        data: {courseId: courseId},
        success: function (result) {
            /*业务处理*/
            // commentContent = JSON.parse(result);
            showComment(result);
        }
    }, 'json');
}

//显示评论
function showComment(result) {
console.log(result)
    let obj = result.data;

    console.log(typeof result)
    let commentNumber = 0;

    if($("#comments .comment-list").html() != null){
        $("#comments .comment-list").html("");
    }

    let commentsTemplate = "";
    for (let i = 0; i < obj.length; i++) {
        commentNumber += 1;
        console.log($(".commentsTemplate").html());
        console.log(commentNumber);
        commentsTemplate = $(".commentsTemplate").html()
            .replaceAll("{{username}}", obj[i].username)
            .replaceAll("{{content}}", obj[i].content)
            .replaceAll("{{commentNumber}}", commentNumber);
        $("#comments").append(commentsTemplate)

    }
}


//提交评论
function toComment(){

    // let username = $("#displayUser").children(0).html().trim();
    let username = JSON.parse(sessionStorage.getItem("User")).username;
    let content = $("#commentContent").val();

    console.log(typeof username);

    console.log(username)
    console.log(content)
    console.log(courseId)

    $.ajax({
        type:"GET",
        url: baseUrl + "education_api/courseComment/insertComment",
        data: {
            username: username,
            content: content,
            courseId: courseId,
        },
        success: function (result) {
            /*业务处理*/
            // commentContent = JSON.parse(result);
            console.log(result);
            alert(result.message);
            comment(courseId);
        }
    }, 'json');

}